/*
 * Copyright 2007-2010 VTT Biotechnology
 * This file is part of GopiBugs.
 *
 * GopiBugs is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * GopiBugs is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GopiBugs; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package PD.modules.simulation.cleanNetwork;

import PD.data.impl.SimpleParameterSet;
import PD.taskcontrol.TaskStatus;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author scsandra
 */
public class ClearNetworkTask {

        private TaskStatus status = TaskStatus.WAITING;
        private String errorMessage;
        private String networkFile;
        private List<Edge> edges;
        private String nodes;

        public ClearNetworkTask(SimpleParameterSet parameters) {
                this.networkFile = (String) parameters.getParameterValue(ClearNetworkParameters.file);
                edges = new ArrayList<Edge>();
        }

        public String getTaskDescription() {
                return "Clear Network... ";
        }

        public double getFinishedPercentage() {
                return 0.0f;
        }

        public TaskStatus getStatus() {
                return status;
        }

        public String getErrorMessage() {
                return errorMessage;
        }

        public void cancel() {
                status = TaskStatus.CANCELED;
        }

        public void run() {
                try {
                        status = TaskStatus.PROCESSING;
                        readNetwork();
                        List<Edge> finalEdges = removeExtraEdges();
                        try {
                                // Create file
                                FileWriter fstream = new FileWriter(this.networkFile + "-final");
                                BufferedWriter out = new BufferedWriter(fstream);
                                out.write(nodes);
                                for (Edge edge : finalEdges) {
                                        out.write("\tedge [\n");
                                        out.write("\t\t" + edge.source + "\n");
                                        out.write("\t\t" + edge.target + "\n");
                                        out.write("\t\t" + edge.label + "\n");
                                        out.write("\t" + "]\n");
                                }
                                //Close the output stream
                                out.close();
                        } catch (Exception e) {//Catch exception if any
                                System.err.println("Error: " + e.getMessage());
                        }

                        status = TaskStatus.FINISHED;
                } catch (Exception e) {
                        e.printStackTrace();
                        status = TaskStatus.ERROR;
                }
        }

        private void readNetwork() {
                try {
                        FileInputStream fstream = new FileInputStream(this.networkFile);
                        DataInputStream in = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new InputStreamReader(in));
                        String strLine;
                        nodes = br.readLine() + "\n";
                        nodes += br.readLine() + "\n";

                        //Read File Line By Line
                        while ((strLine = br.readLine()) != null) {                                
                                if (strLine.contains("node")) {
                                        nodes += strLine + "\n";
                                        for (int i = 0; i < 3; i++) {
                                                nodes += br.readLine() + "\n";
                                        }
                                } else if (strLine.contains("edge")) {
                                        Edge edge = new Edge();
                                        edge.source = br.readLine();
                                        edge.target = br.readLine();
                                        edge.label = br.readLine();
                                        edges.add(edge);
                                }

                        }
                        //Close the input stream
                        in.close();

                } catch (Exception ex) {
                        Logger.getLogger(ClearNetworkTask.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

        private List<Edge> removeExtraEdges() {
                List<Edge> conservedEdges = new ArrayList<Edge>();
                for (Edge edge : edges) {
                        for (Edge edge2 : edges) {
                                if (edge != edge2 &&edge.source.contentEquals(edge2.source)
                                        && edge.target.contentEquals(edge2.target)
                                        && edge.label.contentEquals(edge2.label)) {
                                        conservedEdges.add(edge);
                                        break;
                                }
                        }
                }
                return conservedEdges;
        }

        class Edge {

                String source;
                String target;
                String label;
        }
}
